/**
 * Copyright (c) 2011-2021, James Zhan 詹波 (jfinal@126.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jfinal.server.undertow;

import java.io.File;
import java.util.*;
import java.util.function.UnaryOperator;

public class UndertowKit {

    private UndertowKit() {}

    private static final Collection<String> JAVA_CLASS_PATH = new LinkedHashSet<>();

    static {
        JAVA_CLASS_PATH.addAll(getSystemDefaultClassPath(false));
    }

    public static List<String> getClassPathDirs() {
        final List<String> list = new ArrayList<>();

        JAVA_CLASS_PATH.forEach(classPath -> {
            final File file = new File(classPath);

            if (file.exists() && file.isDirectory()) {
                if (!classPath.endsWith(File.separator)) {
                    classPath = classPath + File.separator;
                }

                list.add(classPath);
            }
        });

        return list;
    }

    public static Collection<String> getJavaClassPath() {
        return JAVA_CLASS_PATH;
    }

    protected static List<String> getSystemDefaultClassPath(boolean checkExists) {
        final List<String> classPaths = new LinkedList<>();

        final UnaryOperator<String> formatPath = path -> {
            path = path.trim();
            path = path.replace('\\', File.separatorChar);
            path = path.replace('/', File.separatorChar);

            if (path.startsWith("./")) {
                path = path.substring(2);
            }

            return path;
        };

        /*
         * unix/linux 与 windows 下 pathSeparator 分别为 ':' 与 ';'
         */
        final String[] classPathArray = System.getProperty("java.class.path").split(File.pathSeparator);
        for (String classPath : classPathArray) {
            classPath = formatPath.apply(classPath);

            final File file = new File(classPath);
            if (!checkExists || file.exists()) {
                classPaths.add(classPath);
            }
        }

        return classPaths;
    }


    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    public static void doNothing(Throwable e) {

    }


    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    public static String getJFinalVersion() {
        try {
            Class<?> clazz = Class.forName("com.jfinal.core.Const");
            java.lang.reflect.Field field = clazz.getField("JFINAL_VERSION");
            return (String) field.get(null);
        } catch (Exception e) {
            return com.jfinal.core.Const.JFINAL_VERSION;
        }
    }
}



